package com.mtsmda.daoImpl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.jws.WebService;
import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mtsmda.daoI.CustomerDAOI;
import com.mtsmda.is.admin.domain.Customer;

@WebService(endpointInterface = "com.mtsmda.daoI.CustomerDAOI")
public class SpCustomerDAOImpl implements CustomerDAOI {

	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public WrapperCustomer getAllCustomers() {
		WrapperCustomer wrapperCustomer = new WrapperCustomer();
		Connection connection = null;
		CallableStatement callableStatement = null;
		ArrayList<Customer> customers = null;

		try {
			connection = dataSource.getConnection();
			callableStatement = connection.prepareCall("call getCustomers()");
			ResultSet resultSet = callableStatement.executeQuery();
			customers = new CustomerDAOImpl().fillList(resultSet);
			wrapperCustomer.setCustomers(customers);
		} catch (Exception e) {
			return wrapperCustomer;
		} finally {
			try {
				if (callableStatement != null && !callableStatement.isClosed()) {
					callableStatement.close();
				}

				if (connection != null && !connection.isClosed()) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return wrapperCustomer;
	}

	@Override
	public Customer getCustomer(Integer customerId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean insertCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteCustomer(Customer customer) {
		// TODO Auto-generated method stub
		return false;
	}

}